package com.sanerzone.smscenter.modules.sms.task;

import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.modules.account.entity.BaseAccount;
import com.sanerzone.smscenter.modules.account.service.BaseAccountService;
import com.sanerzone.smscenter.modules.sms.dao.SmsSendDao;
import com.sanerzone.smscenter.modules.sms.entity.Record;
import com.sanerzone.smscenter.modules.sms.entity.SmsSend;
import com.sanerzone.smscenter.modules.sms.entity.SubmitStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Lazy(false)
public class SmsStatisticsTask {


    public static Logger logger = LoggerFactory.getLogger(SmsStatisticsTask.class);
    @Autowired
    private BaseAccountService jmsgAccountService;

    @Autowired
    private SmsSendDao smsSendDao;

    @Scheduled(cron = "0 0/3 * * * ?")
    @Transactional(readOnly = false)
    public void run(){
        logger.info( "客户端发送情况统计任务开始执行" );
        List<BaseAccount> listAccount = jmsgAccountService.findList(new BaseAccount());
        if(listAccount != null && listAccount.size() > 0){
            for (BaseAccount entity:listAccount){
                String userid = entity.getUserid();

                if (StringUtils.isNotBlank( userid )){
                    Map map=new HashMap( );
                    map.put( "userId",userid );
                    List<Record> records=new ArrayList<>( );
                    //拿到所有当前这个用户属于今天的订单 submitId 做统计
                    List<SubmitStatistics> submitStatistics= smsSendDao.getTodaysSubmitId(map);
                    if (submitStatistics!=null&&submitStatistics.size()>0){
                        for (int i=0;i<submitStatistics.size();i++){
                            SubmitStatistics temp= submitStatistics.get( i );
                            Record submitRecord=new Record();
                            submitRecord.setUserId( userid );
                            submitRecord.setSubmitId(temp.getSubmitId() );
                            submitRecord.setContent( temp.getContent());
                            int failNum=0;
                            int sucNum=0;
                            int unKnownNum=0;
                            int total=0;
                            int waitProcess=0;
                            String tableName=getTableIndex( temp.getSubmitTime() );
                            Map map1=new HashMap( );
                            map1.put( "submitId",temp.getSubmitId() );
                            map1.put( "tableName",tableName );
                            Record record=smsSendDao.getRecordSingleSubmit( map1 );
                            if (record!=null){
                                if (record.getSucCount()!=null){
                                    sucNum=record.getSucCount();
                                }
                                if (record.getFailCount()!=null){
                                    failNum=record.getFailCount();
                                }
                                if (record.getUnKnownCount()!=null){
                                    unKnownNum=record.getUnKnownCount();
                                }
                                if (record.getTotalCount()!=null){
                                    total=record.getTotalCount();
                                }
                            }
                            //找到在待审核  或者待处理的条数
                            List<SmsSend> smsSends=smsSendDao.getWaitProcessOrders( map1 );
                            if (smsSends!=null&&smsSends.size()>0){
                                for (SmsSend smsSend:smsSends){
                                    String phone=smsSend.getPhone();
                                    int length=phone.split( "," ).length;
                                    waitProcess+=length;
                                    total+=length;
                                }
                            }
                            submitRecord.setContent( temp.getContent() );
                            submitRecord.setSubmitTime( temp.getSubmitTime() );
                            submitRecord.setTotalCount( total );
                            submitRecord.setFailCount( failNum );
                            submitRecord.setSucCount( sucNum );
                            submitRecord.setUnKnownCount( unKnownNum );
                            submitRecord.setSubmitId( temp.getSubmitId() );
                            submitRecord.setWaitProcess( waitProcess );
                            records.add( submitRecord );
                        }
                        for (Record temp1:records){
                            SubmitStatistics submitStatistics1=new SubmitStatistics();
                            submitStatistics1.setContent(temp1.getContent());
                            submitStatistics1.setFailCount( temp1.getFailCount() );
                            submitStatistics1.setSubmitId( temp1.getSubmitId() );
                            submitStatistics1.setSubmitTime( temp1.getSubmitTime() );
                            submitStatistics1.setSucCount( temp1.getSucCount() );
                            submitStatistics1.setTaskId( temp1.getTaskIds() );
                            submitStatistics1.setUnknownCount( temp1.getUnKnownCount() );
                            submitStatistics1.setWaitCount( temp1.getWaitProcess() );
                            submitStatistics1.setTotalCount( temp1.getTotalCount() );
                            submitStatistics1.setUserId( temp1.getUserId() );
                            smsSendDao.countStatis( submitStatistics1 );
                        }
                    }
                }
            }
        }
    }
    public String getTableIndex(Date sendDate){
        String tableIndex="";
        SimpleDateFormat sdfYear=new SimpleDateFormat( "yyyy");
        SimpleDateFormat sdfMonth=new SimpleDateFormat( "MM");
        SimpleDateFormat sdfDay=new SimpleDateFormat( "dd");
        Date nowDate=new Date();
        int offset=(int)(sendDate.getTime()-nowDate.getTime())/1000/60/60/24;
        if (offset>=-4){
            //4天以内  直接去找日表

            String sdfDaySearch=sdfDay.format( sendDate );
            if(sdfDaySearch.charAt( 0 )=='0'){
                sdfDaySearch=sdfDaySearch.charAt( 1 )+"";
            }

            tableIndex="sms_send_"+sdfDaySearch;
        }else {
            String year=sdfYear.format( sendDate );
            String month=sdfMonth.format( sendDate );
            //4天以外
            tableIndex="sms_send_history_"+year+month;

        }

        return tableIndex;
    }




}
